package org.ala.tiktools.rpc.kcp.netty;

import io.netty.buffer.ByteBuf;
import io.netty.util.AbstractReferenceCounted;
import io.netty.util.Recycler;
import io.netty.util.ReferenceCounted;

import java.net.InetSocketAddress;

/**
 * @author ala
 * @date 2025-04-14 14:34
 */
public class KcpPacket extends AbstractReferenceCounted {
    /**
     *  线程间共享
     */
    private static final Recycler<KcpPacket> RECYCLER = new Recycler<KcpPacket>() {
        @Override
        protected KcpPacket newObject(Handle<KcpPacket> handle) {
            return new KcpPacket(handle);
        }
    };
    protected Recycler.Handle<KcpPacket> handler;
    private KcpPacket(Recycler.Handle<KcpPacket> handler) {
        this.handler = handler;
    }
    public static KcpPacket newInstance(ByteBuf content, InetSocketAddress remoteAddress) {
        KcpPacket kp = RECYCLER.get();
        kp.setRefCnt(1);
        kp.setContent(content);
        kp.setRemoteAddress(remoteAddress);
        return kp;
    }
    /**
     *  还回
     */
    public void recycle() {
        handler.recycle(this);
    }
 

    /**
     *  内容
     */
    protected ByteBuf content;


	/**
     *  远程地址
     */
    protected InetSocketAddress remoteAddress;


    @Override
    protected void deallocate() {
    	handler.recycle(this);
		content.release();
    }

    @Override
    public ReferenceCounted touch(Object hint) {
        return null;
    }


    public ByteBuf getContent() {
        return content;
    }

    public void setContent(ByteBuf content) {
        this.content = content;
    }

    public InetSocketAddress getRemoteAddress() {
        return remoteAddress;
    }

    public void setRemoteAddress(InetSocketAddress remoteAddress) {
        this.remoteAddress = remoteAddress;
    }
}
